<template>
    <div class="tabs">
        <el-tag size="small" v-for="(item,index) in tags" :key="item.name" :closable="item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'" @click="changeMenu(item)"
            @close="handleClose(item,index)">
            {{item.label}}
        </el-tag>
    </div>
</template>

<script>
import { mapState,mapMutations } from 'vuex';
export default {
    name: 'CommonTag',
    data() {
        return {

        }
    },
    methods: {
        ...mapMutations({
            close:'closeTag'
        }),
        changeMenu(item) {
            this.$router.push({ name: item.name })
        },
        handleClose(tag, index) {
            const length = this.tags.length - 1
            this.close(tag)
            if (tag.name !== this.$route.name) {
                return;
            }
            if (index === length) {
                this.$router.push({ name: this.tags[index - 1].name })
            } else {
                this.$router.push({
                    name: this.tags[index].name
                })
            }
        }
    },
    computed: {
        ...mapState({
            tags: state => state.tab.tabsList
        })
    }
}
</script>

<style lang="scss" scoped>
.tabs {
    padding: 10px;

    .el-tag {
        margin-right: 20px;
        cursor: pointer;
    }
}
</style>